package bite;

/**
 * Creared with IntelliJ IDEA.
 * Description:【红与黑】有一间长方形的房子，地上铺了红色、黑色两种颜色的正方形瓷砖。你站在其中一块黑色的瓷砖上，只能向相邻的（上下左右四个方向）黑色瓷砖移动。
 * 请写一个程序，计算你总共能够到达多少块黑色的瓷砖。
 * User:yxd
 * Date:2022-06-16
 * Time:17:44
 */
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class Pair1{
    int x;
    int y;
    public Pair1(int x,int y){
        this.x = x;
        this.y = y;
    }
}

public class Day38_1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()){
            int row = input.nextInt();
            int col = input.nextInt();
            char[][] ch = new char[row][col];
            for(int i = 0;i < row;i ++){
                String s = input.next();
                for(int j = 0;j < col;j ++){
                    ch[i][j] = s.charAt(j);
                }
            }
            int z = 0;
            int w = 0;
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if(ch[i][j] == '@'){
                        z = i;
                        w = j;
                        break;
                    }
                }
            }
            //放置的队列
            Queue<Pair1> queue = new LinkedList<>();
            //放入起点
            queue.offer(new Pair1(z,w));
            //广度优先遍历
            //四个方向
            int[][] d = {{1,0},{0,1},{-1,0},{0,-1}};
            //标记矩阵
            boolean[][] bool = new boolean[row][col];
            //标记起点
            bool[z][w] = true;
            //记录个数
            int count = 1;
            while (!queue.isEmpty()){
                //当前可走到的所有步数
                int size = queue.size();
                while(size-- != 0){
                    Pair1 p = queue.poll();
                    //当前位置可到达的位置
                    for (int i = 0; i < d.length; i++) {
                        int nx = p.x + d[i][0];
                        int ny = p.y + d[i][1];

                        if(nx < 0 || nx >= row
                                || ny < 0 || ny >= col ){
                            continue;
                        }
                        if(ch[nx][ny] != '#' && !bool[nx][ny]){
                            //当前位置可以走
                            count ++;
                            bool[nx][ny] = true;
                            queue.offer(new Pair1(nx,ny));
                        }
                    }
                }
            }
            System.out.println(count);
        }
    }

    //统计个数
    static int count = 0;
    static int[][] d = {{1,0},{0,1},{-1,0},{0,-1}};
    public static void dfs(char[][] ch,int z,int w,int row,int col){
        if(ch[z][w] == '#'){
            return;
        }
        //当前位置可以走了,将其设置为白格子
        ch[z][w] = '#';
        count ++;
        for(int i = 0;i < 4;i ++){
            int x = z + d[i][0];
            int y = w + d[i][1];
            if(x >= 0 && x < row && y >= 0 && y < col){
                dfs(ch,x,y,row,col);
            }

        }
    }
    public static void main1(String[] args){
        //深度优先搜索
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            int row = input.nextInt();
            int col = input.nextInt();
            char[][] ch = new char[row][col];
            int z = 0;
            int w = 0;
            for (int i = 0; i < row; i++) {
                String s = input.next();
                for (int j = 0; j < col; j++) {
                    ch[i][j] = s.charAt(j);
                    if(ch[i][j] == '@'){
                        z = i;
                        w = j;

                    }
                }
            }
            //使用深度优先遍历
            //每一次都将个数设置为0,重新开始统计
            count = 0;
            dfs(ch,z,w,row,col);
            System.out.println(count);
        }
    }
}
